<?php		
	require_once( trailingslashit( THEME_FUNCTIONS_DIR ) . 'theme-options/home-options.php' );
	require_once( trailingslashit( THEME_FUNCTIONS_DIR ) . 'theme-options/default-options.php' );
	require_once( trailingslashit( THEME_FUNCTIONS_DIR ) . 'wpsocialite/wpsocialite.php' );	
	
	add_action('wp_head', 'cv_home_js');
	function cv_home_js() { 				
		wp_enqueue_script('jquery');?>		
		<script type="text/javascript" language="javascript">
			
	
			jQuery(document).ready(function($){
				$('.hentry .entry-img, #portfolio-list .entry-img').hover(function(){
					 $(this).find('strong').stop().fadeTo('normal', 0.3);
				}, function () {
					$(this).find('strong').stop().fadeTo('normal', 0);
				});	

				
				jQuery(".search-form input").focus(function() { 
					$(this).animate({ width: "180px" }, 1000); 
				}).blur(function() { 
					$(this).animate({ width: "140px" }, 1000); 
				});
		
			});
			
		</script>		
		
	<?php	
	} 
	
	// Variable & intelligent excerpt length.
	function print_excerpt($length = 300) { // Max excerpt length. Length is set in characters
		global $post;
		if(!isset($length)) $length = 300;
		$text = $post->post_excerpt;
		if ( '' == $text ) {
			$text = get_the_content('');
			$text = apply_filters('the_content', $text);
			$text = str_replace(']]>', ']]>', $text);
		}
		$text = strip_shortcodes($text); // optional, recommended
		$text = strip_tags($text); // use ' $text = strip_tags($text,'<p><a>'); ' if you want to keep some tags
		
		$s = '';
		if(strlen($text) > $length)
			$s = '...';
			
		$text = substr($text,0,(int)$length) . $s;
		$excerpt = reverse_strrchr($text, ' ', 1) . $s;
		if( $excerpt ) {			
			echo apply_filters('the_excerpt',$excerpt);
		} else {
			echo apply_filters('the_excerpt',$text);
		}
	}

	// Returns the portion of haystack which goes until the last occurrence of needle
	function reverse_strrchr($haystack, $needle, $trail) {
		return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) + $trail) : false;
	}	
	
	// Tabber: Get Most Popular Posts
	function cv_popular_posts( $order_by = 'comment_count', $posts = 5 ) {
		$popular = new WP_Query('orderby='. $order_by . '&posts_per_page='.$posts);
		while ($popular->have_posts()) : $popular->the_post();
	?>
		<li>
			<div class="feature-img">				
				<?php if ( current_theme_supports( 'get-the-image' ) ) get_the_image( array( 'meta_key' => array( 'Thumbnail', 'thumbnail'), 'default_size' => 'popular-image-thumb', 'default_image' => get_bloginfo('template_directory').'/images/'.'no-pic-small.jpg' ) );?>
			</div>
			<div class="info">
				<h4><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4>	
			
			</div> <!--end .info-->
			<div class="clear"></div>
		</li>
	<?php endwhile; 
	}
	
	
	function get_tag_id_by_name($tag_name) {
		global $wpdb;
		$tag_ID = $wpdb->get_var("SELECT * FROM ".$wpdb->terms." WHERE  `name` =  '".$tag_name."'");

		return $tag_ID;
	}

	/**
	 * Helper functions.
	 */

	/**
	 * Tests if required options are configured to display the Disqus embed.
	 * @return bool
	 */
	function cv_is_installed() {
		return get_option('disqus_forum_url') && get_option('disqus_api_key');
	}

	/**
	 * @return bool
	 */
	function cv_can_replace() {
		global $id, $post;

		if (get_option('disqus_active') === '0'){ return false; }

		$replace = get_option('disqus_replace');

		if ( is_feed() )                       { return false; }
		if ( 'draft' == $post->post_status )   { return false; }
		if ( !get_option('disqus_forum_url') ) { return false; }
		else if ( 'all' == $replace )          { return true; }

		if ( !isset($post->comment_count) ) {
			$num_comments = 0;
		} else {
			if ( 'empty' == $replace ) {
				// Only get count of comments, not including pings.

				// If there are comments, make sure there are comments (that are not track/pingbacks)
				if ( $post->comment_count > 0 ) {
					// Yuck, this causes a DB query for each post.  This can be
					// replaced with a lighter query, but this is still not optimal.
					$comments = get_approved_comments($post->ID);
					foreach ( $comments as $comment ) {
						if ( $comment->comment_type != 'trackback' && $comment->comment_type != 'pingback' ) {
							$num_comments++;
						}
					}
				} else {
					$num_comments = 0;
				}
			}
			else {
				$num_comments = $post->comment_count;
			}
		}

		return ( ('empty' == $replace && 0 == $num_comments)
			|| ('closed' == $replace && 'closed' == $post->comment_status) );
	}

	require_once( trailingslashit( THEME_FUNCTIONS_DIR ) . 'widgets/widget-flickr.php' );
	require_once( trailingslashit( THEME_FUNCTIONS_DIR ) . 'widgets/widget-popular.php' );
	require_once( trailingslashit( THEME_FUNCTIONS_DIR ) . 'widgets/widget-contact.php' );
	require_once( trailingslashit( THEME_FUNCTIONS_DIR ) . 'widgets/widget-twitter.php' );
	require_once( trailingslashit( THEME_FUNCTIONS_DIR ) . 'widgets/widget-testimonial.php' );
	require_once( trailingslashit( THEME_FUNCTIONS_DIR ) . 'widgets/widget-tabs.php' );
	

?>